<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
    <script>
    $(function() {
        $("#search").autocomplete({
        source: "zoekpatient.php",
        minLength: 2
        });
    });
    </script>
<hgroup>
<h1>Bewoners beheren</h1>
</hgroup>

    <br>
<form  name="registerform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
        if (isset($_GET['search']))
        { ?>
            <input placeholder="Achternaam" name="search" id="search" value="<?php echo $_GET['search']; ?>" />
        <?php }
        else
        { ?>
            <input placeholder="Achternaam" name="search" id="search" />
    <?php    }
    ?>
    
    
    <input type="submit" name="Submit" value="Zoeken">
</form>
    <br>
    
Klik <a href="patienttoevoegen.php">hier</a> om een nieuwe bewoner toe te voegen.
<br><br>
Klik <a href="patientdetailoverzicht.php">hier</a> om de mogelijke detail-velden van een bewoner te beheren.
<br><br>
Klik <a href="funcbewoverzicht.php">hier</a> om de functionele bewegingsmogelijkheden van een bewoner te beheren.
<br><br>
Klik <a href="loophulpmiddeloverzicht.php">hier</a> om de mogelijke loophulpmiddelen van een bewoner te beheren.
<br><br>
<?php
if (isset($_GET['start']))
{
    $start=$_GET['start'];
}
else
{
    $start=0;
}

if(strlen($start) > 0 and !is_numeric($start)){
	exit;
}
$eu = ($start - 0);
$limit = 20; // No of records to be shown per page.
$thi = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;

if (isset($_GET['search']))
{
    $page_name="patient.php?search=".$_GET['search']."&";
    if ($_GET['search'] == "")
    {
        $query2="SELECT * from Patient";
    }
    else
    {
        $query2="SELECT * from Patient where LastName='".$_GET['search']."'";
    }
}
else
{
    $page_name="patient.php?";
    $query2="SELECT * from Patient";
}

$result2=mysql_query($query2);
$nume=mysql_num_rows($result2);
?>

<table id="rounded-corner">
    <thead>
    	<tr>
        	<th scope="col" class="rounded-company">Voornaam</th>
            <th scope="col" class="rounded-q1">Achternaam</th>
            <th scope="col" class="rounded-q2">Geslacht</th>
            <th scope="col" class="rounded-q3">Geboortedatum</th>
            <th scope="col" class="rounded-q3">Adres</th>
            <th scope="col" class="rounded-q3">Burgerlijke status</th>
            <th scope="col" class="rounded-q3">Details</th>
            <th scope="col" class="rounded-q4">Bewerken</th>
        </tr>
    </thead>
        <tfoot>
    	<tr>
        	<td colspan="7" class="rounded-foot-left">
        	<?php
	        	if ( $limit < $nume) {
	        		if($back >=0) {
						echo "<a href=".$page_name."start=".$back.">Vorige</a>";
					}
	        		$i=0;
					$l=1;
					for($i=0;$i < $nume;$i=$i+$limit){
						if($i <> $eu)
						{
							echo " <a href=".$page_name."start=".$i.">$l</a> ";
						}
						else 
						{ 
							echo "<span style=\"font-weight:bold;\">".$l."</span>";
						}
						$l=$l+1;
					}
	        		if($thi < $nume) 
	        		{
						echo "<a href=".$page_name."start=".$next.">Volgende</a>";
					}
				}
        	?>
        	</td>
        	<td class="rounded-foot-right">&nbsp;</td>
        </tr>
    </tfoot>
    <tbody>
<?php

if (isset($_GET['search']))
{
    if ($_GET['search'] == "")
    {
        $sql1 = "SELECT * from Patient limit $eu, $limit";
    }
    else 
    {
        $sql1 = "SELECT * from Patient where LastName='".$_GET['search']."' limit $eu, $limit";
    }    
}
else
{
    $sql1 = "SELECT * from Patient limit $eu, $limit";
}



$result1 = mysql_query($sql1) or die (mysql_error());
while($row = mysql_fetch_array($result1))
{

echo "<tr><td>".$row['FirstName']."</td>";
echo "<td>".$row['LastName']."</td>";

if ($row['Gender'] == "V")
{
    echo "<td>Vrouw</td>";
}
else
{
    echo "<td>Man</td>";
}

$dayofbirth = date("d-m-Y",strtotime($row['DayOfBirth']));
echo "<td>" . $dayofbirth."</td>";

$sql2 = "SELECT * FROM Building where Id=".$row['BuildingId']."";
$result2 = mysql_query($sql2) or die (mysql_error());
while($row2 = mysql_fetch_array($result2))
{
    echo "<td>" . $row2['Street']." ".$row2['Number']."</td>";
}

$sql3 = "SELECT * FROM MaritalStatus where Id=".$row['MaritalStatus']."";
$result3 = mysql_query($sql3) or die (mysql_error());
while($row3 = mysql_fetch_array($result3))
{
    echo "<td>" . $row3['Name']."</td>";
}

echo "<td><a href=\"patientdetail.php?id=".$row['Id']."\" >Bekijk details</a></td>";


if (isset($_GET['start'])) {
    if (isset($_GET['search'])) {
        echo "<td><a href=\"patientaanpassen.php?id=" . $row['Id'] . "&search=" . $_GET['search'] . "&start=" . $_GET['start'] . "\" >Bewerken</a></td>";
    } else {
        echo "<td><a href=\"patientaanpassen.php?id=" . $row['Id'] . "&start=" . $_GET['start'] . "\" >Bewerken</a></td>";
    }
} else {
    if (isset($_GET['search'])) {
        echo "<td><a href=\"patientaanpassen.php?id=" . $row['Id'] . "&search=" . $_GET['search'] . "\" >Bewerken</a></td>";
    } else {
        echo "<td><a href=\"patientaanpassen.php?id=" . $row['Id'] . "\" >Bewerken</a></td>";
    }
}

}

?>
</tbody>
</table>

</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>